#!/bin/bash

# 清理所有服务和进程的脚本
# 远程服务器信息
SERVER="root@182.160.6.140"
DEPLOY_PATH="/var/www/binance-analysis"

echo "开始清理所有服务和进程..."

# 停止所有PM2服务
echo "停止所有PM2服务..."
ssh $SERVER "pm2 delete all || true"
sleep 1

# 杀死所有node进程
echo "杀死所有node进程..."
ssh $SERVER "pkill -9 node || true"
sleep 2

# 杀死所有PM2守护进程
echo "杀死所有PM2守护进程..."
ssh $SERVER "ps aux | grep 'PM2' | grep -v grep | awk '{print \$2}' | xargs -r kill -9"
sleep 1

# 确保PM2不会自启动
echo "禁用PM2自启动..."
ssh $SERVER "pm2 unstartup || true"
sleep 1

# 确认所有监听端口都已释放
echo "检查端口是否已释放..."
ssh $SERVER "lsof -i:3000,3001,3002 || echo '所有端口已释放'"

# 确认没有残留的node进程
echo "检查是否还有node进程..."
ssh $SERVER "ps aux | grep -v grep | grep node || echo '没有发现node进程'"

echo "清理完成！系统现在处于干净状态。" 